#!/bin/bash
# 使用此脚本需要确保/usr/local/src目录下有php-7.2.34.tar.gz文件

# 安装php
yum -y remove php php-fpm
yum -y install libxml2-devel libjpeg-devel libpng-devel freetype-devel curl-devel openssl-devel php-mysql sqlite-devel libonig-dev libicu-devel
cd /usr/local/src
tar -zxf php-7.2.34.tar.gz
cd php-7.2.34/
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-fpm --with-fpm-user=nginx --with-fpm-group=nginx --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-gd --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --with-libzip --enable-soap --without-pear --with-gettext --disable-fileinfo --enable-maintainer-zts
make
make install

# 创建php配置文件 
cp php.ini-production /usr/local/php/etc/php.ini		# php运行核心配置文件
cd /usr/local/php/etc/
cp -a php-fpm.conf.default ./php-fpm.conf			# php-fpm主配置文件
cp php-fpm.d/www.conf.default ./php-fpm.d/www.conf		# php-fpm扩展配置文件
echo 'export PATH=$PATH:/usr/local/php/bin' >> /etc/profile	# 添加环境变量
source /etc/profile

# 添加启动管理文件
sed -i 's/^;pid/pid/' /usr/local/php/etc/php-fpm.conf		# 指定pid文件
cat >> /usr/lib/systemd/system/php-fpm.service <<-'EOF'
[Unit]
Description=The PHP FastCGI Process Manager
After=syslog.target network.target

[Service]
Type=forking
PIDFile=/usr/local/php/var/run/php-fpm.pid
ExecStart=/usr/local/php/sbin/php-fpm
ExecReload=/bin/kill -USR2 $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

# 启动php-fpm
systemctl daemon-reload
systemctl start php-fpm
systemctl enable php-fpm
ss -naltp | grep 9000
